//package cn.lxh.basic.interceptor;
//
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.stereotype.Component;
//import org.springframework.util.StringUtils;
//import org.springframework.web.servlet.HandlerInterceptor;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.PrintWriter;
//import java.util.concurrent.TimeUnit;
//
//@Component
//public class LoginInterceptor implements HandlerInterceptor {
//    @Autowired
//    private RedisTemplate redisTemplate;
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        //1.从请求头中获取 U-TOKEN
//        String token = request.getHeader("U-TOKEN");
//        if(StringUtils.hasLength(token)){
//            //3.根据U-TOKEN从redis中获取登录信息
//            Object loginTmp = redisTemplate.opsForValue().get(token);
//            if(loginTmp != null){
//                //刷新过期时间--在半个小时中填写表格没写完。
//                redisTemplate.opsForValue().set(
//                        token,
//                        loginTmp,
//                        30,
//                        TimeUnit.MINUTES
//                );
//                return true;
//            }
//        }
//        //登录页面
//        response.setContentType("application/json;charset=utf-8");
//        response.setCharacterEncoding("utf-8");
//        PrintWriter writer = response.getWriter();
//        //拦截了后端生了write了一个错误 nologin的message交给第三个拦截器
//        writer.write("{\"success\":false,\"message\":\"noLogin\"}");
//        writer.flush();
//        writer.close();
//        return false;//阻止放行
//
//    }
//}
